<?php defined('BASEPATH') OR exit('No direct script access allowed');

	class useradmin_class extends CI_Model {
	
		/*
		 Get User DATA
		*/
		public function get_useradmin_data($post){
			if(isset($post['intUserID'])){
				$intUserID = stripslashes(filter_var($post['intUserID'], FILTER_SANITIZE_STRING));
				$this->db->where(array('intID'=>$intUserID));
			}
			if(isset($post['intID'])){
				$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
				$this->db->where(array('intID'=>$intID));
			}
			$query = $this->db->get('jtn_users')->result_array();
			$query = current($query);
		
			if(trim($query['intID'])=="") {
				return 0;
			} else {
				return $query;
			}
		}
		
		public function list_usersadmin_data($post){
			if(isset($post['intUserID'])){
				$intUserID = stripslashes(filter_var($post['intUserID'], FILTER_SANITIZE_STRING));
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_users',array('intAccountType'=>1,'intDisabled'=>0,'intID'=>$intUserID));
			} else {
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_users',array('intAccountType'=>1,'intDisabled'=>0));
			}
			if ($query->num_rows() > 0){
				$result = $query->result_array();
				return $result;
			}
			else{
				return 0;
			}
		}		
		public function update($post) {
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			$strUserName = stripslashes(filter_var($post['strUserName'], FILTER_SANITIZE_STRING));
			$strBirthDay = stripslashes(filter_var($post['strBirthDay'], FILTER_SANITIZE_STRING));
			$strUserEmail = stripslashes(filter_var($post['strUserEmail'], FILTER_SANITIZE_STRING));
			$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
			$ageUser = stripslashes(filter_var($post['ageUser'], FILTER_SANITIZE_STRING));
			$strPassword = pw_hash(stripslashes(filter_var($post['strPassword'], FILTER_SANITIZE_STRING)));
			
			$data = array(
					'strName' => $strUserName,
					'dateBirthDay' => $strBirthDay,
					'strEmail' => $strUserEmail,
					'strImgLink' => $strImgLink,
					'intAge' => $ageUser,
					'strPassword' => $strPassword,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_users', $data);
				
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		
		public function add_useradmin($post) {
			$strUserName = stripslashes(filter_var($post['strUserName'], FILTER_SANITIZE_STRING));
			$strBirthDay = stripslashes(filter_var($post['strBirthDay'], FILTER_SANITIZE_STRING));
			$strUserEmail = stripslashes(filter_var($post['strUserEmail'], FILTER_SANITIZE_STRING));
			$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
			$ageUser = stripslashes(filter_var($post['ageUser'], FILTER_SANITIZE_STRING));
			$password = pw_hash(stripslashes(filter_var($post['strPassword'], FILTER_SANITIZE_STRING)));
		
			$data = array(
					'strName' => $strUserName,
					'dateBirthday' => $strBirthDay,
					'strEmail' => $strUserEmail,
					'strImgLink' => $strImgLink,
					'intAge' => $strImgLink,
					'strPassword' => $password,
					'intAccountType' => 1,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
			$this->db->insert('jtn_users', $data);
				
			$lastID = $this->db->insert_id();
		
			$data2 = array(
					'intUserID' => $lastID,
					'intPoints' => 0,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
			$this->db->insert('jtn_users_points', $data2);
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		

		public function delete($post) {
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'intDisabled' => 1,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_users', $data);
		
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully deleted.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}

		public function add_useradmin_points($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			$intPoints = stripslashes(filter_var($post['strUserPoint'], FILTER_SANITIZE_STRING));
				
			$data = array(
					'intUserID'=>$intID,
					'intPoints'=>$intPoints,
					'intPointsID'=> 1,
					'strCreatedBy'=>$this->session->userdata('intID'),
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
						
			);
				
			$this->db->insert('jtn_users_points',$data);
				
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully deleted.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		public function change_permadmin($post){
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'intAccountType' => 0,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_users', $data);
		
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully deleted.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		
	} // end class
?>